/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.TbSender;
import com.thinkgem.jeesite.modules.sys.service.TbSenderService;
import com.thinkgem.jeesite.modules.sys.utils.DesUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 配送员管理Controller
 * @author huoyan403
 * @version 2016-11-09
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/tbSender")
public class TbSenderController extends BaseController {

	@Autowired
	private TbSenderService tbSenderService;
	
	@ModelAttribute
	public TbSender get(@RequestParam(required=false) String id) {
		TbSender entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbSenderService.get(id);
		}
		if (entity == null){
			entity = new TbSender();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(TbSender tbSender, HttpServletRequest request, HttpServletResponse response, Model model) {
		String uid = UserUtils.getUser().getId();
		tbSender.setId(uid);
		Page<TbSender> page = tbSenderService.findPage(new Page<TbSender>(request, response), tbSender); 
		model.addAttribute("page", page);
		if(page.getList().size()==0){
			model.addAttribute("message1","无查询结果");
		}
		return "modules/sys/tbSenderList";
	}

	@RequestMapping(value = "form")
	public String form(TbSender tbSender, Model model) {
		tbSender.setPassword("");
		model.addAttribute("tbSender", tbSender);
		return "modules/sys/tbSenderForm";
	}

	@RequestMapping(value = "save")
	public String save(TbSender tbSender, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbSender)){
			return form(tbSender, model);
		}
		String uid = UserUtils.getUser().getId();
		Integer shopId=tbSenderService.findShopidByUid(uid);
		tbSender.setShopId(shopId);
		try {
			DesUtils util=new DesUtils();
			if(tbSender.getPassword()!=null&&!tbSender.getPassword().equals("")){
				    tbSender.setPassword(util.encrypt(tbSender.getPassword()));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		tbSender.setRegistrationId("");
		tbSenderService.save(tbSender);
		addMessage(redirectAttributes, "保存配送员成功");
		return "redirect:"+Global.getAdminPath()+"/sys/tbSender";
	}
	
	@RequestMapping(value = "delete")
	public String delete(TbSender tbSender, RedirectAttributes redirectAttributes) {
		tbSenderService.delete(tbSender);
		addMessage(redirectAttributes, "删除配送员成功");
		return "redirect:"+Global.getAdminPath()+"/sys/tbSender";
	}
	//验证派送员手机号的唯一性
	@ResponseBody
	@RequestMapping(value = "checkSenderByMobile")
	public String checkSenderByMobile(String mobile,String id) {
		TbSender tbSender=tbSenderService.checkSenderByMobile(mobile);
		if(tbSender!=null){
			if(tbSender.getId().equals(id)){
				return "true";
			}
			return "false";
		}else{
			return "true";
		}

	}

}